//
//  LoginViewController.m
//  Daybook
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "LoginViewController.h"
#import "Common.h"
#import "RegisterViewController.h"
#import "RegisterWithEmailViewController.h"
#import <BmobSDK/Bmob.h>
#import "ResetPasswordViewController.h"
#import "ResetPasswordWithEmailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface LoginViewController ()

@property (nonatomic, strong)UITextField *userTextField;

@property (nonatomic, strong)UITextField *passwordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}

- (void)createView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 34, 25, 25);
    [self.view addSubview:backButton];
    backButton.backgroundColor = [UIColor clearColor];

    [backButton setImage:[UIImage imageNamed:@"backArrow"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    UIButton *registerButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(MAIN_WIDTH - 50, 34, 40, 25);
    registerButton.backgroundColor = [UIColor whiteColor];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    
    
    self.userTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, MAIN_WIDTH - 20, 40)];
    self.userTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userTextField];
    
    self.userTextField.placeholder = @"用户名/邮箱/手机号";
    
    
    self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 140, MAIN_WIDTH - 20, 40)];
    self.passwordTextField.backgroundColor = [UIColor whiteColor];
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextField];
    
    

    
    
    UIView *firstLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, MAIN_WIDTH, 1)];
    firstLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:firstLineView];
    
    UIView *secLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 140, MAIN_WIDTH, 1)];
    secLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:secLineView];
    
    
    UIView *thirdLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 180, MAIN_WIDTH, 1)];
    thirdLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:thirdLineView];
    
    
    UIButton *loginButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(10, 200, MAIN_WIDTH - 20, 40);
    loginButton.backgroundColor  = [UIColor orangeColor];
    [self.view addSubview:loginButton];
    
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(clickLogin:) forControlEvents:UIControlEventTouchUpInside];
    loginButton.layer.cornerRadius = 5;
    loginButton.layer.masksToBounds = YES;
    
    
    UIButton *resetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    resetPasswordButton.backgroundColor = [UIColor clearColor];
    resetPasswordButton.frame = CGRectMake(MAIN_WIDTH / 2 - 40, 260, 80, 40);
    [resetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [resetPasswordButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.view addSubview:resetPasswordButton];
    
    [resetPasswordButton addTarget:self action:@selector(clickResetPassword:) forControlEvents:UIControlEventTouchUpInside];
    

    
    
}

- (void)clickRegister:(UIButton *)button {
//    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
//    registerVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    [self presentViewController:registerVC animated:YES completion:^{
//        
//        
//    }];
    
    
    RegisterWithEmailViewController *registerWithEmailVC = [[RegisterWithEmailViewController alloc]init];
    registerWithEmailVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [self presentViewController:registerWithEmailVC animated:YES completion:^{
        
        
    }];
    
    
}

- (void)clickLogin:(UIButton *)button {
    
    NSString *account = self.userTextField.text;
    NSString *password = self.passwordTextField.text;
    
    [BmobUser loginInbackgroundWithAccount:account andPassword:password block:^(BmobUser *user, NSError *error) {
        if (user) {
            BmobFile *file = (BmobFile *)[user objectForKey:@"image"];
            [self dismissViewControllerAnimated:YES completion:^{
                
                
                
            }];
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:file.url]]];
            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"headChanged" object:image];
        } else {
            NSLog(@"%@",error);
            

                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入正确的用户名和密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                }];
                
                [alertController addAction:alertAction];
                [self presentViewController:alertController animated:YES completion:^{
                    
                    
                }];
                

            
            
            
        }
    }];
    
    
}

- (void)clickResetPassword:(UIButton *)button {
    
//    ResetPasswordViewController *resetPasswordVC = [[ResetPasswordViewController alloc]init];
//    [self presentViewController:resetPasswordVC animated:YES completion:^{
//        
//        
//    }];
    
    ResetPasswordWithEmailViewController *resetPasswordWithEmailVC = [[ResetPasswordWithEmailViewController alloc]init];
    [self presentViewController:resetPasswordWithEmailVC animated:YES completion:^{
        
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickBack:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
